/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.order;

import com.x3.purchasing.account.Account;
import com.x3.purchasing.common.ClassAntiNull;
import com.x3.purchasing.common.ClassApplicationModule;
import com.x3.purchasing.operator.OperatorDAO;
import com.x3.purchasing.operator.OperatorDAOImpl;
import com.x3.purchasing.produk.ProdukDAO;
import com.x3.purchasing.produk.ProdukDAOImpl;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author kris
 */
public class CartWnd extends ClassApplicationModule {

    private OrderDAO orderDAO;
    private OrderDetailDAO orderDetailDAO;
    private ProdukDAO produkDAO;
    private OperatorDAO operatorDAO;
    private Session session;
    private Listbox listboxData;
    private Textbox txtTotal;
    private Account account;
    private DecimalFormat df = new DecimalFormat("#0,000.00");

    public void onCreate() {
        orderDAO = new OrderDAOImpl();
        orderDetailDAO = new OrderDetailDAOImpl();
        produkDAO = new ProdukDAOImpl();
        operatorDAO = new OperatorDAOImpl();
        txtTotal = (Textbox) this.getFellow("txtTotal");
        listboxData = (Listbox) this.getFellow("listboxData");
        this.loadDataProduk();
        session = Sessions.getCurrent();
        account = (Account) session.getAttribute("ACC_LOGIN");
    }

    public void loadDataProduk() {
        listboxData.getItems().clear();
        session = Sessions.getCurrent();
        if (session.getAttribute("ORDER") != null) {
            final List<OrderDetail> orderDetails = (List<OrderDetail>) session.getAttribute("ORDER");
            int index = 1;
            double total = 0;
            txtTotal.setValue("");
            for (final OrderDetail orderDetail : orderDetails) {
                Listitem listitem = new Listitem();
                listitem.appendChild(new Listcell(index + ""));
                listitem.appendChild(new Listcell(orderDetail.getProduk().getKode()));
                listitem.appendChild(new Listcell(orderDetail.getProduk().getNama()));
                listitem.appendChild(new Listcell(df.format(orderDetail.getHargaSatuan())));
                listitem.appendChild(new Listcell(orderDetail.getJumlah() + ""));
                //listitem.appendChild(new Listcell(orderDetail.getJumlah() + ""));
                listitem.appendChild(new Listcell(df.format(orderDetail.getHargaSatuan() * orderDetail.getJumlah())));
                final Toolbarbutton btnDelete = new Toolbarbutton();
                btnDelete.setId(index + "");
                btnDelete.setTooltiptext("Klik untuk menghapus data");
                btnDelete.setImage("/img/delete.png");
                btnDelete.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        delete(Integer.valueOf(btnDelete.getId()) - 1);
                    }
                });

                Listcell cellAksi = new Listcell();
                cellAksi.appendChild(btnDelete);
                listitem.appendChild(cellAksi);
                listboxData.appendChild(listitem);
                total += orderDetail.getHargaSatuan() * orderDetail.getJumlah();
                txtTotal.setValue(df.format(total));
                index++;
            }
        }
    }

    private void delete(int index) {
        List<OrderDetail> orderDetails = (List<OrderDetail>) session.getAttribute("ORDER");
        orderDetails.remove(index);
        this.loadDataProduk();
    }

    public void cancel() {
        if (session.getAttribute("ORDER") != null) {
            List<OrderDetail> orderDetails = (List<OrderDetail>) session.getAttribute("ORDER");
            orderDetails.removeAll(orderDetails);
            this.loadDataProduk();
        }
    }

    public void save() throws InterruptedException {
        List<OrderDetail> orderDetails = (List<OrderDetail>) session.getAttribute("ORDER");
        if (session.getAttribute("ORDER") != null) {
            if (orderDetails.size() > 0) {
                String orderId = new Date().getTime() + "";
                Order order = new Order();
                order.setId(orderId);
                order.setTanggal(new Date());
                order.setOperator(orderDetails.get(0).getOrder().getOperator());
                try {
                    orderDAO.insert(order);
                    for (OrderDetail orderDetail : orderDetails) {
                        orderDetail.setOrder(order);
                        orderDetailDAO.insert(orderDetail);
                    }
                    Messagebox.show("Data order telah disimpan");
                    session.removeAttribute("ORDER");
                    this.loadDataProduk();
                    txtTotal.setValue("");
                } catch (Exception ex) {
                    Logger.getLogger(CartWnd.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            Messagebox.show("Anda tidak punya daftar belanja");
        }
    }
}
